﻿/* 
 *  <copyright file="CGuid.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;
using SobrietyEngine.Common;
using SobrietyEngine.Logging;

namespace SobrietyEngine.UDMF
{
    public static class UDMFStringReader
    {
        
        /*
         * This is a lazy way to load the format where i essentially
         * just load the map data and ignore the rest of the file...to be improved later...
         * 
         * */
        
        public static UDMFMap Load(String data)
        {
            UDMFMap map = new UDMFMap();

            //lets get all our data between the first and last lines
            //the first contains wad lump data
            //and the last contains wad lump data
            //neither of which we need here
            int firstNL = data.IndexOf("namespace");
            
            int lastNL = data.Length - 1;
            lastNL = data.LastIndexOf("}");

            //the data between the two newlines
            string mid = data.Substring(firstNL + 1, lastNL - (firstNL + 1));

            //runs through the UDMF formated text and loads all items
            StringData sd = StringData.ReadData(ref mid);
            
            //lets iterate through each item in the stringdata we just loaded form the UDMF file
            foreach (StringData sdat in sd.SubTokens)
            {
                String token = sdat.Token;
                Dictionary<string, string> kvps = sdat.Data;
                
                BaseUDMFObject obj = null;

                if (token == "vertex")
                {
                    Vertex vert = new Vertex();
                    map.Verts.Add(vert);
                    obj = vert;
                }
                else if (token == "linedef")
                {
                    Linedef ld = new Linedef();
                    map.Linedefs.Add(ld);
                    obj = ld;
                }
                else if (token == "sidedef")
                {
                    Sidedef t = new Sidedef();
                    map.Sidedefs.Add(t);
                    obj = t;
                }
                else if (token == "sector")
                {
                    Sector t = new Sector();
                    map.Sectors.Add(t);
                    obj = t;
                }
                else if (token == "thing")
                {
                    Thing t = new Thing();
                    map.Things.Add(t);
                    obj = t;
                }

                if (obj != null)
                {
                    obj.Number = map.Verts.Count;
                    obj.Map = map;
                    obj.Read(kvps);
                }
                else
                    Logger.GetSingleton().WriteDebug("Object type " + token + " unhandled type in UDMF map");
            }

            return map;
        }

    }
}
